#!/bin/bash
#
#

set -e

COLOUR_1="echo -en \E[1;39m"
COLOUR_2="echo -en \E[1;30m"
COLOUR_3="echo -en \E[1;34m"
COLOUR_ERROR="echo -en \E[1;31m"
COLOUR_END="echo -en \E[0;00m"

function usage() {
    ${COLOUR_1}
    echo "This script (${0}) is used to generate a Diamond Girl distribution."
    ${COLOUR_2}
    echo " - inputs version number, and additions to the Changelog"
    echo " - update the version number in configure.ac"
    echo " - determines the distribution date&time"
    echo " - updates Changelog"
    echo " - build the distribution packages"
    echo " - generate the web page(s)"
    echo " - outputs ftp commands to upload files to the web page"
    echo " - does NOT commit changes to the Subversion repository"
    echo ""
    ${COLOUR_1}
    echo "Usage: ${0} <version> <filename>"
    ${COLOUR_2}
    echo " <version>  The version number, for example \"1.0.5\"."
    echo " <filename> The name of the file containing new additions to the Changelog."
    echo ""
    ${COLOUR_1}
    echo "Some considerations about the version number:"
    ${COLOUR_2}
    echo "* If there are only fixes, do a patch release number update (x.y.z -> x.y.z+1)."
    echo "* If there are new features, do at least revision number update (x.y.z -> x.y+1.0), or major version update (x.y.z -> x+1.0.0)"
    echo ""
    ${COLOUR_1}
    echo "Example of the contents of the file:"
    ${COLOUR_2}
    echo "* Something was changed."
    echo "* Something else was changed too."
    echo "No extra lines, one will be added automatically."
    ${COLOUR_END}
}

function show_error() {
    ${COLOUR_1}
    echo -n "${0}:"
    ${COLOUR_ERROR}
    echo " ${1}"
    ${COLOUR_END}
    echo ""
}

function show_status() {
    if [ ${CURRENT_PHASE} -gt 0 ]; then
        echo ""
    fi
    CURRENT_PHASE=$((CURRENT_PHASE+1))
    ${COLOUR_1}
    echo -n "${0}:"
    ${COLOUR_2}
    echo -n " (${CURRENT_PHASE}/14)"
    ${COLOUR_3}
    echo " ${1}"
    ${COLOUR_END}
}


if [ ${#} -ne 2 ]; then
    show_error "Wrong number of arguments."
    usage
    exit 1
fi

# Determine some values
VERSION=${1}
FILENAME=${2}
DISTDATE="$(date --utc)"
# Make some of them available to the other scripts
export VERSION
export DISTDATE
export MAKEFLAGS=-s

# Some checking
TMPFN=${0}.tmp
if [ -f ${TMPFN} ]; then
    show_error "The file \"${TMPFN}\" exists, it is being used for temporary storage and is being overwritten. Please delete it."
    exit 1
fi

if [ ! -f ${FILENAME} ]; then
    show_error "File \"${FILENAME}\" not found error."
    exit 1
fi

CURRENT_PHASE=0

# Update antivirus database
show_status "Updating antivirus database"
which freshclam > /dev/null
if [ ${?} -ne 0 ]; then
    show_error "Please make sure 'clamav' and 'clamav-update' are installed."
    show_error "# yum install clamav clamav-update && usermod -G clamupdate -a $(whoami) && chmod g+w /var/lib/clamav"
    exit 1
fi
freshclam

# Update configure.ac
show_status "Updating version number to configure.ac"
echo "AC_INIT([Diamond Girl], [${VERSION}], [joniyrjana@gmail.com], [diamond-girl], [http://www.netikka.net/joyr/diamond_girl/])" > ${TMPFN}
tail --lines=+2 configure.ac >> ${TMPFN}
mv ${TMPFN} configure.ac


# Update Changelog
show_status "Updating Changelog from ${FILENAME}"
echo "Release ${VERSION} on ${DISTDATE}" > ${TMPFN}
cat ${FILENAME} >> ${TMPFN}
echo "" >> ${TMPFN}
cat Changelog >> ${TMPFN}
mv ${TMPFN} Changelog

# Build distribution packages
show_status "Building distribution packages (1/5): ./autogen.sh"
./autogen.sh
show_status "Building distribution packages (2/5): ./configure"
./configure
show_status "Building distribution packages (3/5): make dist"
make dist
show_status "Building distribution packages (4/5): ./build-win32-installer.sh"
./build-win32-installer.sh
show_status "Building distribution packages (5/5): check for viruses"
clamscan diamond-girl-${VERSION}.exe diamond-girl-${VERSION}.tar.bz2 2>&1 | tee html/clamscan.out

show_status "Building web page"
cat html/header.html > html/index.html
for i in logo about screenshots features ; do
    cat html/${i}.html >> html/index.html
done
for i in latest_release dist_source dist_win32 ; do
    ./html/${i}.sh >> html/index.html
done
for i in  dist_gentoo building footer ; do
    cat html/${i}.html >> html/index.html
done

show_status "Displaying FTP commands to do the update"
${COLOUR_2}
echo "bi"
echo "tick"
echo "cd diamond_girl"
echo "put diamond-girl-${VERSION}.tar.bz2"
echo "put diamond-girl-${VERSION}.exe"
echo "put Changelog"
echo "put html/index.html index.html"
echo "put html/index.css index.css"
echo "ls"

show_status "Deleting ${FILENAME}"
rm ${FILENAME}

show_status "Adding new files to SVN"
svn add diamond-girl-${VERSION}.exe diamond-girl-${VERSION}.tar.bz2

show_status "Dumping SVN status"
svn status | grep -v ^?

show_status "Done."
${COLOUR_END}
echo ""

